/*
 * Copyright (c) 2015, the Dart project authors.
 *
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vm.element

// This file is generated by the script: pkg/vm_service/tool/generate.dart in dart-lang/sdk.

import com.google.gson.JsonArray
import com.google.gson.JsonObject

/**
 * The {@link Stack} class represents the various components of a Dart stack trace for a given
 * isolate.
 */
typealias VmStack = Stack

class Stack(json: JsonObject) : Response(json) {

    /**
     * A list of frames representing the asynchronous path. Comparable to `awaiterFrames`, if
     * provided, although some frames may be different.
     *
     * Can return <code>null</code>.
     */
    fun getAsyncCausalFrames(): ElementList<Frame>? {
        if (json.get("asyncCausalFrames") == null) return null

        return object : ElementList<Frame>(json.get("asyncCausalFrames").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): Frame {
                return Frame(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * A list of frames representing the asynchronous path. Comparable to `asyncCausalFrames`, if
     * provided, although some frames may be different.
     *
     * Can return <code>null</code>.
     */
    fun getAwaiterFrames(): ElementList<Frame>? {
        if (json.get("awaiterFrames") == null) return null

        return object : ElementList<Frame>(json.get("awaiterFrames").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): Frame {
                return Frame(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * A list of frames that make up the synchronous stack, rooted at the message loop (i.e., the
     * frames since the last asynchronous gap or the isolate's entrypoint).
     */
    fun getFrames(): ElementList<Frame> {
        return object : ElementList<Frame>(json.get("frames").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): Frame {
                return Frame(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * A list of messages in the isolate's message queue.
     */
    fun getMessages(): ElementList<Message> {
        return object : ElementList<Message>(json.get("messages").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): Message {
                return Message(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * Specifies whether or not this stack is complete or has been artificially truncated.
     */
    fun getTruncated(): Boolean {
        return getAsBoolean("truncated")
    }
}
